const conn = require('../data/gy_index');
module.exports.gy_userAdd = (req, res) => {
   if (!req.session.isgaoyuan) {
      res.redirect('/gy_login');
      return;
   }
   res.render('gy_userAdd')
}
module.exports.gy_CRM = (req, res) => {
   if (!req.session.isgaoyuan) {
      res.redirect('/gy_login');
      return;
   }
   res.render('gy-CRM');
}
// #region
// 显示页面
module.exports.gy_business = (req, res) => {
   if (!req.session.isgaoyuan) {
      res.redirect('/gy_login');
      return;
   }
   res.render('gy_business');
}
module.exports.gy_contract = (req, res) => {
   if (!req.session.isgaoyuan) {
      res.redirect('/gy_login');
      return;
   }
   res.render('gy_contract');
}
module.exports.gy_product = (req, res) => {
   if (!req.session.isgaoyuan) {
      res.redirect('/gy_login');
      return;
   }
   res.render('gy_product');
}
module.exports.gy_sale = (req, res) => {
   if (!req.session.isgaoyuan) {
      res.redirect('/gy_login');
      return;
   }
   res.render('gy_sale');
}
module.exports.gy_seas = (req, res) => {
   if (!req.session.isgaoyuan) {
      res.redirect('/gy_login');
      return;
   }
   res.render('gy_seas');
}
module.exports.gy_thread = (req, res) => {
   if (!req.session.isgaoyuan) {
      res.redirect('/gy_login');
      return;
   }
   res.render('gy_thread');
}
module.exports.tmeplate = (req, res) => {
   if (!req.session.isgaoyuan) {
      res.redirect('/gy_login');
      return;
   }
   res.render('tmeplate');
}
// #endregion




// 添加用户
module.exports.gy_useradd = (req, res) => {
   // console.log(req.body.scale6);
   let sql = 'insert into clients(gy_clientsUsers,gy_clientsCompany,gy_clientsRank,gy_clientsPhone,gy_clientsTelephone,gy_clientsState,gy_clientsdata) values(?,?,?,?,?,?,?)';
   let data = [req.body.clientt, req.body.name, req.body.scale2, req.body.cellphone, req.body.call, req.body.scale6, req.body.birthday];
   conn.query(sql, data, (eer, results) => {
      if (eer) return console.log(eer);
      res.json({
         code: '1004',
         message: '用户添加成功'
      })
   })
}
// 查看用户
module.exports.showcrm = (req, res) => {
   // console.log(req.query);
   conn.query('select * from clients', (eer, results) => {
      if (eer) return console.log(eer);
      res.json(results);
   })
}
// 删除用户
module.exports.crmDelete = (req, res) => {
   conn.query('delete from clients where gy_id=' + req.query.id, (eer, results) => {
      if (eer) return console.log(eer);
      res.json({
         code: '1005',
         message: '用户删除成功'
      })
   })
}
// 搜索
module.exports.userecho = (req, res) => {
   // console.log(req.query );
   // console.log('select * from clients where gy_clientsUsers like "%'+req.query.addname+'%"');
   conn.query('select * from clients where gy_clientsUsers like "%' + req.query.addname + '%"', (eer, results) => {
      if (eer) return console.log(eer);
      res.json(results);
   })
}
// 编辑
module.exports.gy_echo = (req, res) => {
   // res.render('gy_echo');
   conn.query('select * from clients where gy_id=' + req.query.id, (eer, results) => {
      if (eer) return console.log(eer);
      // console.log(results);
      res.render('gy_echo', results[0]);
   })
}
module.exports.gy_userupdate = (req, res) => {
   let sql = 'update clients set gy_clientsUsers=?,gy_clientsCompany=?,gy_clientsRank=?, gy_clientsPhone=?,gy_clientsTelephone=?,gy_clientsState =?,gy_clientsdata=? where gy_id=?';
   let datastr = [req.body.clientt, req.body.name, req.body.scale2, req.body.cellphone, req.body.call, req.body.scale6, req.body.birthday, req.body.id]
   conn.query(sql, datastr, (eer, results) => {
      if (eer) return console.log(eer);
      res.json({
         code: '1007',
         message: '修改成功'
      })
   })



}